package com.seu.githubcn.utils;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;

public class Utils {
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();

    }

    public static Git fromCloneRepository(String repoUrl, String cloneDir, CredentialsProvider provider){
        Git git = null;
        try {
            git = Git.cloneRepository()
                    .setCredentialsProvider(provider)
                    .setURI(repoUrl)
                    .setDirectory(new File(cloneDir)).call();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        return git;
    }

    public static Git forkRepository(String repoUrl, String cloneDir, CredentialsProvider provider){
        Git git = null;
        try {
            git = Git.cloneRepository()
                    .setCredentialsProvider(provider)
                    .setURI(repoUrl)
                    .setBare(true)
                    .setDirectory(new File(cloneDir)).call();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        return git;
    }

    public static String constructLocalPath(String basePath,String[] urlSlides,int i){
        StringBuilder localPath = new StringBuilder(basePath);//仓库的服务器所在位置
        for (; i < urlSlides.length; i++) {
            localPath.append(urlSlides[i]);
            localPath.append("/");
        }
        return String.valueOf(localPath);
    }



    public static boolean deleteByPath(String path){
        File file = new File(path);
        if(!file.exists()){
            return false;
        }
        if(file.isFile()){
            return file.delete();
        }
        File[] files = file.listFiles();
        if(files==null){
            return file.delete();
        }
        for (File f : files) {
            if(f.isFile()){
                if(!f.delete()){
                    System.out.println(f.getAbsolutePath()+" delete error!");
                    return false;
                }
            }else{
                if(!deleteByPath(f.getAbsolutePath())){
                    return false;
                }
            }
        }
        return file.delete();
    }

    public static Boolean checkTag(Git git, String tag){
        List<Ref> tags = null;
        try {
            tags = git.tagList().call();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        if(tags!=null){
            for(var t : tags){
                if(t.getName().equals(tag)){
                    return true;
                }
            }
        }
        return false;
    }

    public static Boolean checkBranch(Git git, String branch){
        try {

            var refs = git.branchList().call();
            if(refs==null){
                return false;
            }
            for (var ref : refs) {
                if (!ref.getName().contains(branch)) {
                    return false;
                }
            }
            return true;
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static Git openRepo(String dir){
        Git git = null;
        try {
            Repository repository = new FileRepositoryBuilder()
                    .setGitDir(Paths.get(dir, ".git").toFile())
                    .build();
            git = new Git(repository);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return git;
    }

    public static void deleteDir(String dirPath) {
        File file = new File(dirPath);
        if (!file.isFile()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File value : files) {
                    deleteDir(value.getAbsolutePath());
                }
            }
        }
        System.out.println(file.delete());
    }
}
